Smoothed, not ribbon for banff
#
# incucyte_dt[, value_norm := value/value[Elapsed==0], by=c('well','day','donor','car','measurement','k562','image')]
# incucyte_dt[, mean_value_norm := mean(value_norm), by=c('day','donor','car','Elapsed','measurement','k562')]
# incucyte_dt[, std_dev := sd(value_norm), by=c('day','donor','car','Elapsed','measurement','k562')]
# incucyte_dt[, std_error := std_dev/sqrt(.N), by=c('day','donor','car','Elapsed','measurement','k562')]
car_set <- c('41BB','CD28','BAFF-R','KLRG1','TACI', 'none')
colors <- c(brewer.pal(9,'Blues')[c(6,8)], brewer.pal(9,'Greens')[c(5,7,9)], 'grey30')
incucyte_dt_subset <- incucyte_dt[
k562=='mkate_cd19' & !noise & t_type == 'cd4' &
measurement == chosen_measure & car %in% car_set
& donor < 3 ][,
car := factor(
car,
levels=c('CD28', '41BB', 'BAFF-R', 'KLRG1', 'TACI', 'none'),
labels=c('CD28', '41BB', 'Receptor 1', 'Receptor 3', 'Receptor 4', 'No T cells')
)]
cd4_banff <- ggplot(incucyte_dt_subset,
aes(x=Elapsed, y=mean_value_norm, group=interaction(car, day_f,donor), fill=car)) +
geom_smooth(aes(color=car), method='loess', se=T) +
facet_grid(t_type+donor~day_f) +
theme_minimal(base_size=18) + geom_vline(xintercept=0) + geom_hline(yintercept=0) +
theme(plot.title = element_text(size = 25)) +
scale_x_continuous(expand=c(0,0), limits=c(0,150), breaks=c(0, 40, 80, 120)) +
scale_y_continuous('mKate Total Integrated Intensity\n(normalized to starting)',
limits=c(1e-2,1e1), trans=log10_trans(), labels = scales::percent_format(accuracy = 1)) +
theme(plot.title = element_text(hjust = 0.5)) +
scale_color_manual(values=colors) +
scale_fill_manual(values=colors) +
theme(panel.spacing = unit(0.5, "lines"))
incucyte_dt_subset <- incucyte_dt[
k562=='mkate_cd19' & !noise & t_type == 'cd8' &
measurement == chosen_measure & car %in% car_set
& donor < 3 ][,
car := factor(
car,
levels=c('CD28', '41BB', 'BAFF-R', 'KLRG1', 'TACI', 'none'),
labels=c('CD28', '41BB', 'Receptor 1', 'Receptor 3', 'Receptor 4', 'No T cells')
)]
cd8_banff <- ggplot(incucyte_dt_subset,
aes(x=Elapsed, y=mean_value_norm, group=interaction(car, day_f,donor), fill=car)) +
geom_smooth(aes(color=car), method='loess', se=T) +
facet_grid(t_type+donor~day_f) +
theme_minimal(base_size=18) + geom_vline(xintercept=0) + geom_hline(yintercept=0) +
theme(plot.title = element_text(size = 25)) +
scale_x_continuous(expand=c(0,0), limits=c(0,65), breaks=c(0, 15, 30, 45)) +
scale_y_continuous('mKate Total Integrated Intensity\n(normalized to starting)',
limits=c(1e-2,1e1), trans=log10_trans(), labels = scales::percent_format(accuracy = 1)) +
theme(plot.title = element_text(hjust = 0.5)) +
scale_color_manual(values=colors) +
scale_fill_manual(values=colors) +
theme(panel.spacing = unit(0.5, "lines"))
library(cowplot)
##
## ********************************************************
## Note: As of version 1.0.0, cowplot does not change the
## default ggplot2 theme anymore. To recover the previous
## behavior, execute:
## theme_set(theme_cowplot())
## ********************************************************
plot_grid(cd4_banff, cd8_banff, ncol=1)
## Warning: Transformation introduced infinite values in continuous y-axis
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 6570 rows containing non-finite values (stat_smooth).
## Warning: Transformation introduced infinite values in continuous y-axis
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 1521 rows containing non-finite values (stat_smooth).

% killing plots
bb28z_dt <- incucyte_dt[car %in% c('41BB','CD28','zeta') & k562 != 'none' &
noise==F]
setnames(bb28z_dt, "car", "ctrl_car")
# function to get elapsed time at which red measurement crosses threshold and
# get some plots out
k562_thresh <- function(incucyte_dt, pct_rem){
cross_dt <- incucyte_dt[Elapsed<=150, list(
t_cross = min(Elapsed[order(abs(mean_value_none-(pct_rem/100)))][1:15]),
cross_thresh = min(mean_value_none)<(pct_rem/100),
last_mean_val_none = last(mean_value_none)),
by=c('car','k562','donor','measurement','day','t_type','noise')][
noise==F & cross_thresh==T]
cross_dt[, day_f := factor(cross_dt$day, levels=c('0','8','15','22','29'))]
cd4_thresh <- ggplot(incucyte_dt[t_type=='cd4' & measurement==chosen_measure &
noise==F & k562=='mkate_cd19' & car!='none']) +
geom_line(aes(x=Elapsed, y=mean_value_none,
group=interaction(well,day,donor),
color=as.factor(donor))) +
geom_vline(data=cross_dt[measurement==chosen_measure & k562=='mkate_cd19' &
t_type=='cd4' & car!='none'],
aes(xintercept=t_cross)) +
facet_grid(car~day_f+donor) +
geom_line(data=bb28z_dt[t_type=='cd4' & measurement==chosen_measure &
k562=='mkate_cd19'],
aes(x=Elapsed, y=mean_value_none,
group=interaction(well,day,donor,ctrl_car),
linetype=ctrl_car),
color='gray') +
geom_text(data=cross_dt[t_type=='cd4' & measurement==chosen_measure &
k562=='mkate_cd19' & car!='none'],
aes(x=t_cross-8, y=1.5, label=t_cross)) +
scale_x_continuous(expand=c(0,0), limits=c(0,150)) +
scale_y_continuous('mKate Total Integrated Intensity\n(normalized to growth without CAR)',
limits=c(1e-2,1e1), trans=log10_trans()) +
theme_minimal(base_size=20) +
geom_vline(xintercept=0) +
geom_hline(yintercept=0) +
theme(plot.title = element_text(size = 20, hjust = 0.5))+
labs(title=paste0("CD4 - Time When ", chosen_measure, " Reaches ", pct_rem,
"% of Starting"),
fill='Donor',
color='Donor',
linetype='Controls')
cd8_thresh <- ggplot(incucyte_dt[t_type=='cd8' & measurement==chosen_measure &
noise==F & k562=='mkate_cd19' & car!='none']) +
geom_line(aes(x=Elapsed, y=mean_value_none,
group=interaction(well,day,donor),
color=as.factor(donor))) +
geom_vline(data=cross_dt[measurement==chosen_measure & k562=='mkate_cd19' &
t_type=='cd8' & car!='none'],
aes(xintercept=t_cross)) +
facet_grid(car~day_f+donor) +
geom_line(data=bb28z_dt[t_type=='cd8' & measurement==chosen_measure &
k562=='mkate_cd19'],
aes(x=Elapsed, y=mean_value_none,
group=interaction(well,day,donor,ctrl_car),
linetype=ctrl_car),
color='gray') +
geom_text(data=cross_dt[t_type=='cd8' & measurement==chosen_measure &
k562=='mkate_cd19' & car!='none'],
aes(x=t_cross-8, y=1.5, label=t_cross)) +
scale_x_continuous(expand=c(0,0), limits=c(0,70)) +
scale_y_continuous('mKate Total Integrated Intensity\n(normalized to growth without CAR)',
limits=c(1e-2,1e1), trans=log10_trans()) +
theme_minimal(base_size=20) +
geom_vline(xintercept=0) +
geom_hline(yintercept=0) +
theme(plot.title = element_text(size = 20, hjust = 0.5)) +
labs(title=paste0("CD8 - Time When ", chosen_measure, " Reaches ", pct_rem,
"% of Starting"),
fill='Donor',
color='Donor',
linetype='Controls')
new_list <- list(cross_dt, cd4_thresh, cd8_thresh)
return(new_list)
}
cross_60 <- k562_thresh(incucyte_dt, 60)
cross_60[[1]]
cross_60[[2]]
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 11601 row(s) containing missing values (geom_path).
## Warning: Removed 4986 row(s) containing missing values (geom_path).
## Warning: Removed 17 rows containing missing values (geom_text).

cross_60[[3]]
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 2608 row(s) containing missing values (geom_path).
## Warning: Removed 994 row(s) containing missing values (geom_path).
## Warning: Removed 2 rows containing missing values (geom_text).

# barplots showing time when 60% red intensity is reached
# 2020.05.04 - KK - still some issues with this... for some CARs, the code is
# not taking the first time at which the threshold is crossed but times that
# have lowest abs value. need to fix.
ggplot(cross_60[[1]][t_type=='cd4' & measurement==chosen_measure & k562=='mkate_cd19'], aes(x=car, y=t_cross, fill=car)) +
geom_bar(stat="identity", position=position_dodge()) +
geom_text(aes(x=car, y=t_cross+5, group=t_cross, label=t_cross),
position = position_dodge(width=0.9),
size=4) +
facet_grid(donor~day_f,
labeller=label_both) +
theme_minimal(base_size=20) +
geom_hline(yintercept=-Inf) +
geom_vline(xintercept=-Inf) +
scale_y_continuous(expand=c(0,0), limits=c(0,220)) +
scale_fill_brewer(palette='Paired') +
theme(plot.title=element_text(size=20, hjust=0.5),
panel.spacing=unit(2, "lines"),
axis.text.x=element_blank(),
axis.title.x=element_blank()) +
labs(title='CD4 - Time When Red Intensity Reaches 60% of Starting',
y='Hours When 60% Red Intensity Remains',
fill='CAR')

ggplot(cross_60[[1]][t_type=='cd8' & measurement==chosen_measure & k562=='mkate_cd19'], aes(x=car, y=t_cross, fill=car)) +
geom_bar(stat="identity", position=position_dodge()) +
geom_text(aes(x=car, y=t_cross+2, group=t_cross, label=t_cross),
position = position_dodge(width=0.9),
size=4) +
facet_grid(donor~day_f,
labeller=label_both) +
theme_minimal(base_size=20) +
geom_hline(yintercept=-Inf) +
geom_vline(xintercept=-Inf) +
scale_y_continuous(expand=c(0,0), limits=c(0,53)) +
scale_fill_brewer(palette='Paired') +
theme(plot.title=element_text(size=20, hjust=0.5),
panel.spacing=unit(2, "lines"),
axis.text.x=element_blank(),
axis.title.x=element_blank()) +
labs(title='CD8 - Time When Red Intensity Reaches 60% of Starting',
y='Hours When 60% Red Intensity Remains',
fill='CAR')

# line plots with elapsed time when threshold is reached.
# again, same note as above chunk -- need to be fixed
# need to add ggrepel to these
ggplot(incucyte_dt[k562=='mkate_cd19' & !noise & t_type == 'cd4' & measurement == chosen_measure & car!='none'],
aes(x=Elapsed, y=mean_value_none, group=interaction(car, day_f,donor), fill=car)) +
geom_line(aes(color=car)) +
geom_text(data=cross_60[[1]][k562=='mkate_cd19' & t_type=='cd4' &
measurement==chosen_measure & car!='none'],
aes(label=t_cross, colour=car, x=140, y=last_mean_val_none)) +
facet_grid(t_type+donor~day_f) +
ggtitle("Incucyte K562 Killing by mKate Total Integrated Intensity (normalized to starting)") +
theme_minimal(base_size=20) + geom_vline(xintercept=0) + geom_hline(yintercept=0) +
theme(plot.title = element_text(size = 25)) +
scale_x_continuous(expand=c(0,0), limits=c(0,150)) +
scale_y_continuous('mKate Total Integrated Intensity\n(normalized to growth without CAR)',
limits=c(1e-2,1e1), trans=log10_trans()) +
theme(plot.title = element_text(hjust = 0.5)) +
scale_color_brewer(palette='Paired') +
scale_fill_brewer(palette='Paired') +
geom_line(data=incucyte_dt[k562=='mkate_cd19' & !noise & t_type=='cd4' & measurement==chosen_measure & car=='none'], aes(x=Elapsed, y=mean_value_none),
color='gray') +
theme(panel.spacing = unit(2, "lines"))
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 11601 row(s) containing missing values (geom_path).
## Warning: Removed 4 rows containing missing values (geom_text).
## Warning: Removed 1908 row(s) containing missing values (geom_path).

ggplot(incucyte_dt[k562=='mkate_cd19' & !noise & t_type == 'cd8' & measurement == chosen_measure],
aes(x=Elapsed, y=mean_value_none, group=interaction(car, day_f,donor), fill=car)) +
geom_line(aes(color=car)) +
facet_grid(t_type+donor~day_f) +
ggtitle("Incucyte K562 Killing by mKate Total Integrated Intensity (normalized to starting)") +
theme_minimal(base_size=20) + geom_vline(xintercept=0) + geom_hline(yintercept=0) +
theme(plot.title = element_text(size = 25)) +
scale_x_continuous(expand=c(0,0), limits=c(0,70)) +
scale_y_continuous('mKate Total Integrated Intensity\n(normalized to growth without CAR)',
limits=c(1e-2,1e1), trans=log10_trans()) +
theme(plot.title = element_text(hjust = 0.5)) +
scale_color_brewer(palette='Set1') +
scale_fill_brewer(palette='Set1') +
theme(panel.spacing = unit(2, "lines"))
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 2698 row(s) containing missing values (geom_path).

# facet on car and color by day
my_colors <- brewer.pal(n=9, "PuBuGn")[5:9]
ggplot(incucyte_dt[k562=='mkate_cd19' & !noise & t_type == 'cd4' & measurement == chosen_measure & car!='none'],
aes(x=Elapsed, y=mean_value_none, group=interaction(car, day_f,donor), fill=day_f)) +
geom_line(aes(color=day_f)) +
facet_grid(t_type+donor~car) +
ggtitle("CD4 - Incucyte K562 Killing by mKate Total Integrated Intensity (normalized to starting)") +
theme_minimal(base_size=20) + geom_vline(xintercept=0) + geom_hline(yintercept=0) +
theme(plot.title = element_text(size = 25)) +
scale_x_continuous(expand=c(0,0), limits=c(0,150)) +
scale_y_continuous('mKate Total Integrated Intensity\n(normalized to growth without CAR)',
limits=c(1e-2,1e1), trans=log10_trans()) +
theme(plot.title = element_text(hjust = 0.5)) +
scale_color_manual(values=my_colors) +
scale_fill_manual(values=my_colors) +
geom_hline(yintercept=1,
color='gray60') +
theme(panel.spacing = unit(2, "lines"))
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 11601 row(s) containing missing values (geom_path).

ggplot(incucyte_dt[k562=='mkate_cd19' & !noise & t_type == 'cd8' & measurement == chosen_measure & car!='none'],
aes(x=Elapsed, y=mean_value_none, group=interaction(car, day_f,donor), fill=day_f)) +
geom_line(aes(color=day_f)) +
facet_grid(t_type+donor~car) +
ggtitle("CD8 - Incucyte K562 Killing by mKate Total Integrated Intensity (normalized to starting)") +
theme_minimal(base_size=20) + geom_vline(xintercept=0) + geom_hline(yintercept=0) +
theme(plot.title = element_text(size = 25)) +
scale_x_continuous(expand=c(0,0), limits=c(0,70)) +
scale_y_continuous('mKate Total Integrated Intensity\n(normalized to growth without CAR)',
limits=c(1e-2,1e1), trans=log10_trans()) +
theme(plot.title = element_text(hjust = 0.5)) +
scale_color_manual(values=my_colors) +
scale_fill_manual(values=my_colors) +
geom_hline(yintercept=1,
color='gray60') +
theme(panel.spacing = unit(2, "lines"))
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 2608 row(s) containing missing values (geom_path).

# % cancer cells killed at different timepoints - theo's suggestion at meeting
set_time <- function(df, elapsed_hr){
cd4_plot <- ggplot(incucyte_dt[k562=='mkate_cd19' & !noise & t_type == 'cd4' &
measurement == chosen_measure & car!='none' &
Elapsed==elapsed_hr],
aes(x=car, y=mean_value_none, group=interaction(car, day_f, donor),
fill=car, color=car)) +
geom_point(aes(color=car)) +
geom_linerange(aes(ymin=mean_value_none-std_dev_value_none_log,
ymax=mean_value_none+std_dev_value_none_log)) +
facet_grid(t_type+donor ~ day_f) +
ggtitle(paste0("CD4 - Incucyte K562 Killing by mKate Total Integrated Intensity\n(normalized to starting) at ", elapsed_hr ," Hours")) +
theme_minimal(base_size=20) +
theme(plot.title = element_text(size = 25)) +
scale_y_continuous('mKate Total Integrated Intensity\n(normalized to growth without CAR)',
limits=c(1e-2,1e1), trans=log10_trans()) +
theme(plot.title = element_text(hjust = 0.5)) +
scale_color_brewer(palette='Paired') +
scale_fill_brewer(palette='Paired') +
geom_hline(yintercept=1,
color='gray60') +
theme(panel.spacing = unit(2, "lines"),
axis.text.x = element_text(angle = 90, vjust=0.5))
cd8_plot <- ggplot(incucyte_dt[k562=='mkate_cd19' & !noise & t_type == 'cd8' &
measurement == chosen_measure & car!='none' &
Elapsed==elapsed_hr],
aes(x=car, y=mean_value_none, group=interaction(car, day_f, donor),
fill=car, color=car)) +
geom_point(aes(color=car)) +
geom_linerange(aes(ymin=mean_value_none-std_dev_value_none_log,
ymax=mean_value_none+std_dev_value_none_log)) +
facet_grid(t_type+donor ~ day_f) +
ggtitle(paste0("CD8 - Incucyte K562 Killing by mKate Total Integrated Intensity\n(normalized to starting) at ", elapsed_hr ," Hours")) +
theme_minimal(base_size=20) +
theme(plot.title = element_text(size = 25)) +
scale_y_continuous('mKate Total Integrated Intensity\n(normalized to growth without CAR)',
limits=c(1e-2,1e1), trans=log10_trans()) +
theme(plot.title = element_text(hjust = 0.5)) +
scale_color_brewer(palette='Paired') +
scale_fill_brewer(palette='Paired') +
geom_hline(yintercept=1,
color='gray60') +
theme(panel.spacing = unit(2, "lines"),
axis.text.x = element_text(angle = 90, vjust=0.5))
new_list <- list(cd4_plot, cd8_plot)
return(new_list)
}
set_time(incucyte_dt, 26)[[1]]
## Warning in self$trans$transform(x): NaNs produced
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 24 rows containing missing values (geom_segment).
## Warning: Removed 9 rows containing missing values (geom_segment).

set_time(incucyte_dt, 26)[[2]]
## Warning in self$trans$transform(x): NaNs produced
## Warning: Transformation introduced infinite values in continuous y-axis
## Warning: Removed 4 rows containing missing values (geom_segment).
## Warning: Removed 36 rows containing missing values (geom_segment).
## Warning: Removed 24 rows containing missing values (geom_segment).
## Warning: Removed 8 rows containing missing values (geom_segment).
## Warning: Removed 36 rows containing missing values (geom_segment).
## Warning: Removed 36 rows containing missing values (geom_segment).
## Warning: Removed 18 rows containing missing values (geom_segment).
## Warning: Removed 30 rows containing missing values (geom_segment).
